<template>
  <view class="settings-page">
    <view class="settings-list">
      <!-- 头像 -->
      <view class="settings-item" @tap="handleAvatarTap">
        <text class="item-label">头像</text>
        <view class="item-right">
          <image class="avatar" :src="DEFAULT_IMAGE" mode="aspectFill" />
          <view class="arrow-icon"></view>
        </view>
      </view>

      <!-- 昵称 -->
      <view class="settings-item" @tap="handleNicknameTap">
        <text class="item-label">昵称</text>
        <view class="item-right">
          <text class="item-value">{{ userStore.userInfo.user.nickName }}</text>
          <view class="arrow-icon"></view>
        </view>
      </view>

      <!-- 手机号码 -->
      <view class="settings-item" @tap="handlePhoneTap">
        <text class="item-label">手机号码</text>
        <view class="item-right">
          <text class="item-value">{{ userStore.userInfo.user.phonenumber }}</text>
          <view class="arrow-icon"></view>
        </view>
      </view>

      <!-- 添加预约到日历 -->
      <view class="settings-item">
        <text class="item-label">添加预约到日历</text>
        <switch
          :checked="calendarEnabled"
          @change="handleSwitchChange"
          color="#4CD964"
          class="switch-style"
        />
      </view>
    </view>

    <!-- 退出登录按钮 -->
    <view class="logout-btn" @tap="handleLogout">
      退出登录
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { DEFAULT_IMAGE } from '../../config/images'
import { useUserStore } from '../../stores/user'

const calendarEnabled = ref(false)
const userStore = useUserStore()
const handleAvatarTap = () => {
  // 处理头像点击
  uni.chooseImage({
    count: 1,
    success: (res) => {
      console.log(res.tempFilePaths)
    }
  })
}

const handleNicknameTap = () => {
  // 处理昵称点击
  uni.navigateTo({
    url: '/pages/my/nickname'
  })
}

const handlePhoneTap = () => {
  // 处理手机号点击
  uni.navigateTo({
    url: '/pages/my/phone'
  })
}

const handleSwitchChange = (e: any) => {
  calendarEnabled.value = e.detail.value
}

const handleLogout = () => {
  uni.showModal({
    title: '提示',
    content: '确定要退出登录吗？',
    success: (res) => {
      if (res.confirm) {
        // 处理退出登录逻辑
        uni.clearStorageSync()
        uni.reLaunch({
          url: '/pages/index/index'
        })
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.settings-page {
  min-height: 100vh;
  background: #F3F3FD;
  padding: 20rpx;
}

.settings-list {
  background: #fff;
  border-radius: 24rpx;
  overflow: hidden;
  margin-bottom: 40rpx;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 32rpx;
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
  
  &:last-child {
    border-bottom: none;
  }

  .item-label {
    font-size: 28rpx;
    color: #333;
  }

  .item-right {
    display: flex;
    align-items: center;
    
    .avatar {
      width: 80rpx;
      height: 80rpx;
      border-radius: 50%;
      margin-right: 16rpx;
    }

    .item-value {
      font-size: 28rpx;
      color: #999;
      margin-right: 16rpx;
    }

    .arrow-icon {
      width: 12rpx;
      height: 12rpx;
      border-top: 2rpx solid #999;
      border-right: 2rpx solid #999;
      transform: rotate(45deg);
    }
  }

  .switch-style {
    transform: scale(0.8);
  }
}

.logout-btn {
  background: #4e6ef2;
  color: #fff;
  text-align: center;
  padding: 28rpx 0;
  border-radius: 10rpx;
  font-size: 32rpx;
  margin-top: 40rpx;
}
</style>
